﻿#include "TopWidget.h"
#include<qfile.h>
#include<QKeyEvent>
#include<qaction.h>
TopWidget::TopWidget(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	//QSize modfied(5, 5);
	//ui.btn_close->setFixedSize(ui.btn_close->iconSize() + modfied);
	//ui.btn_minimize->setFixedSize(ui.btn_minimize->iconSize() + modfied);
	//ui.btn_maximize->setFixedSize(ui.btn_maximize->iconSize() + modfied);

	QFile f("TopWidget.qss");
	if (f.open(QIODeviceBase::ReadOnly)) {
		QString body = QLatin1String(f.readAll());
		this->setStyleSheet(body);
	}
	

	connect(ui.btn_close, &QPushButton::clicked, [this]() {
		emit closePlayer();
		});

	connect(ui.btn_minimize, &QPushButton::clicked, [this]() {
		emit minimizePlayer();
		});

	connect(ui.btn_maximize, &QPushButton::clicked, [this]() {
		emit maximizePlayer();
		});
	initSearchEdit();
}

TopWidget::~TopWidget()
{}

void TopWidget::setBtnMaxIcon()
{
	ui.btn_maximize->setIcon(QIcon(":/icons/huanyuan.png"));
}

void TopWidget::setBtnMaxNormalIcon()
{
	ui.btn_maximize->setIcon(QIcon(":/icons/zuidahua.png"));
}

void TopWidget::moveButtonsMouseTrack()
{
	auto children = this->children();
	for (auto& i : children)
	{
		QPushButton* btn = qobject_cast<QPushButton*>(i);
		if (btn)
		{
			btn->setMouseTracking(false);
		}
	}
}

void TopWidget::initSearchEdit()
{
	QAction *searchAction = new QAction(ui.searchEdit);
	searchAction->setIcon(QIcon("icons/sousuo.png"));
	ui.searchEdit->addAction(searchAction,QLineEdit::ActionPosition::TrailingPosition);
	ui.searchEdit->setPlaceholderText("搜索");

	connect(ui.searchEdit, &QLineEdit::returnPressed, 
		[this] {
			emit searchFinished(ui.searchEdit->text());
			ui.searchEdit->clear();
		});
}

#ifdef QT_DEBUG
void TopWidget::keyPressEvent(QKeyEvent* ev)
{
	if (ev->key() == Qt::Key_F) {
		// 重新加载样式表文件
		QFile file("TopWidget.qss"); // 使用资源文件路径
		if (file.open(QFile::ReadOnly)) {
			QString qss = QLatin1String(file.readAll());
			this->setStyleSheet(qss); // 应用到当前窗口或 QApplication::setStyleSheet(qss);
			file.close();
		}
	}
	QWidget::keyPressEvent(ev); // 调用基类处理
}
#endif


